-- Bar
-- 物品栏
Bar = class()

function Bar:init()
    self.pos = 1
    self.t = false
    self.s = -5
    self.pointCount = 9
    self.items = {}
    -- 初始物品
    self:setItem(1,Object.Tennis)
    self:setItem(2,Object.BaseBall)
    self:setItem(3,Object.rubber)
    self:setItem(4,Object.TNT)
    self:setItem(5,Object.WoodenBox)
    self:setItem(6,Object.ice)
    self:setItem(7,Object.weight)
    self:setItem(8,Object.Jet)
    self:setItem(9,Object.Bowling)
end

function Bar:draw()
    rectMode(CENTER)
    pushStyle()
    fill(122, 113, 116, 137)
    for k = 1, self.pointCount do
        sprite("Project:seletorA",(WIDTH - 45), (-25 + 80 * k), (80 + self.s), (80 + self.s))
    end
    pushStyle()
    tint(41, 41, 41, 255)    
    sprite("Project:seletorA", (WIDTH - 45), (-25 + 80 * self.pos), (80 + self.s), (80 + self.s))
    popStyle()
    for k,v in pairs(self.items) do
        sprite(v.item.icon, (WIDTH - 45), (-25 + 80 * v.pos),50,50)
        -- text(v.item.name,(WIDTH - 215), (55 + 80 * self.pos))
    end
    popStyle()
end

function Bar:touched(touch)
    for p = 1,self.pointCount do
        if self:isTouchInPos(touch,p) then
            self.pos = p
            return true
        end
    end
    return false
end

function Bar:isTouchInPos(touch,point)
    return touch.x > math.abs((WIDTH - 45) - 40) and touch.x<math.abs((WIDTH - 45) + 40) and touch.y > math.abs((-25 + 80 * point) - 40) and touch.y < math.abs((-25 + 80 * point) + 40)
end

function Bar:setItem(i,object)
    if self:getItem(i) then
        for k,v in pairs(self.items) do
        if v.pos == i then
            v.item = object
            return
        end
    end
    else
        table.insert(self.items,{pos = i,item = object})
    end
end

function Bar:getItem(i)
    for k,v in pairs(self.items) do
        if v.pos == i then
            return v.item
        end
    end
    return nil
end

function Bar:getCurrentItem()
    return self:getItem(self.pos)
end